<?php
function get_menu(){
	/*
		&nbsp;<a href=\"index.php?type=timeline&filter=ham\">ham</a>
		&nbsp;<a href=\"index.php?type=timeline&filter=spam\">spam</a>
	*/
	return "&nbsp;<a href=\"index.php?type=timeline\">".__('All', true)."</a>
			&nbsp;(<a href=\"index.php?type=timeline&filter=ham\">".__('Ham', true)."</a>
			&nbsp;<a href=\"index.php?type=timeline&filter=spam\">".__('Spam', true)."</a>)
			&nbsp;<a href=\"index.php?type=mentions\">".__('Mentions', true)."</a>
			&nbsp;<a href=\"index.php?type=favs\">".__('Favs', true)."</a> |
			&nbsp;<a href=\"inbox.php\">".__('Inbox', true)."</a>
			&nbsp;<a href=\"sent.php\">".__('Sent', true)."</a> |
			&nbsp;<a href=\"list.php\">".__('List', true)."</a>
			&nbsp;<a href=\"index.php?type=profile\">".__('Profile', true)."</a> | 
			&nbsp;<a href=\"setpass.php\">".__('Logout', true)."</a><hr />";
}

function get_post_form($tweet_text = null,$reply_id=null){
	$tweet_text = isset($tweet_text) ? $tweet_text : '';
	$tweet_text_count = mb_strlen($tweet_text);
	$tweet_text_count = ($tweet_text_count == 0) ? 140 : $tweet_text_count;
	$return_str="";
	$return_str=$return_str."<FORM id=\"AddTweetForm\" METHOD=\"post\" ACTION=\"post.php\">\n";
	$return_str=$return_str."<TEXTAREA NAME=\"text\" ROWS=\"1\" COLS=\"20\">".$tweet_text."</TEXTAREA>\n";
	if(!is_null($reply_id)){
		$return_str=$return_str."<INPUT TYPE=\"hidden\" NAME=\"reply_id\" VALUE=\"".$reply_id."\">\n";
	}
	$return_str=$return_str."<INPUT TYPE=\"submit\" value=\"".__('Post', true)."\" class=\"submit\">\n";
	$return_str=$return_str."<a href=\"img.php\">".__('Upload an img', true)."</a>\n";
	$return_str=$return_str."<span id=\"word_count\">".$tweet_text_count."</span></FORM>";
	return $return_str;
}

function showPostDMForm($screen_name){
	return "<FORM METHOD=\"post\" ACTION=\"post_dm.php\">
    UserName:<INPUT TYPE=\"text\" NAME=\"user_name\" value=\"".$screen_name."\">\n
	Message:<INPUT TYPE=\"text\" NAME=\"text\">\n
	<INPUT TYPE=\"submit\" value=\"".__('Send', true)."\">\n
</FORM>";
}

function twitter_parse_links_callback($matches) {
  $url = $matches[1];
  $encoded_url=my_url_encode($url);
  return "<a href='b.php?g=".$encoded_url."' target='blank'>".$url."</a>";
}

function search_links_callback($matches) {
  $q = $matches[3];
  return "<a href='search.php?q=".$q."'>#".$q."</a>";
}

function twitter_parse_tags($input) {
  $out = preg_replace_callback('#(\w+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)(?<![.,])#is', 'twitter_parse_links_callback', $input);
  $out = preg_replace('#(^|\s)@([a-z_A-Z0-9]+)#', '$1@<a href="user.php?screen_name=$2">$2</a>', $out);  
  $out = preg_replace_callback('#(^|\s)(\\#([a-z_A-Z0-9:_-]+))#', 'search_links_callback', $out);
  return $out;
}

function show_action($screen_name,$type,$page,$id,$favorited,$ham_or_spam){
	echo '<a href="response.php?mode=reply&id='.$id.'">'.__('Reply', true).'</a>&nbsp;';
	echo '<a href="response.php?mode=retweet&id='.$id.'">'.__('Retweet', true).'</a>&nbsp;';
	
	if($screen_name==USER){
		echo '<a href="del.php?type='.$type.'&page='.$page.'&id='.$id.'">'.__('Delete', true).'</a>&nbsp;';
	} else {
		if($favorited=="1"){
			echo '<a href="fav.php?ftype=unfav&type='.$type.'&page='.$page.'&id='.$id.'">'.__('Unfav', true).'</a>&nbsp;';
		} else {
			echo '<a href="fav.php?ftype=fav&type='.$type.'&page='.$page.'&id='.$id.'">'.__('Fav', true).'</a>&nbsp;';
		}
	}
	
	echo '| <span class="rating">'.__(ucfirst($ham_or_spam), true).'</span>&nbsp;';
	
	if($ham_or_spam == 'spam') {
		echo '<a href="learn.php?id='.$id.'&mode=ham">'.__('Ham', true).'</a>&nbsp;';		
	}
	elseif($ham_or_spam == 'ham') {
		echo '<a href="learn.php?id='.$id.'&mode=spam">'.__('Spam', true).'</a>&nbsp;';
	}
}

function show_color_div($i, $ham_or_spam){
	if($i%2==0){
		echo '<div class="tweet even '.$ham_or_spam.'">';	
	} else {
		echo '<div class="tweet odd '.$ham_or_spam.'">';
	}
}

function show_msg($i,$msg,$type,$page,$tweets_cache){
	if(!is_array($msg)){
		__('Password error. '.sprintf('<a href="setpass.php">%s</a>', __('Reset', true)));
		exit();
	}
	$screen_name=$msg['user']['screen_name'];
	$favorited=$msg['favorited'];
	$id=$msg['id'];	
	$parsed = twitter_parse_tags($msg['text']);
	$created_at = date_create($msg['created_at']);
	$created_at = time_since(date_format($created_at, 'U'));

	//is the tweet cached?
	if(!$ham_or_spam = get_tweet_from_cache($tweets_cache, $id)) {
				
		$ham_or_spam = classify_tweet($screen_name, $parsed);
	
		save_tweet_to_cache($id, $ham_or_spam);
	}	
	
	show_color_div($i, $ham_or_spam);
	
	echo "<img src='".$msg['user']['profile_image_url']."'>";		
	echo "<a class='screen-name' href='user.php?screen_name=".$screen_name."'>".$screen_name."</a> | ";
	show_action($screen_name,$type,$page,$id,$favorited,$ham_or_spam);
	echo ' | '.$created_at;	
	echo '<div class="text">'.$parsed.'</div>';
	echo '</div>';
}

function show_search_msg($i,$msg,$type,$page){
	show_color_div($i,"ham");
	echo "<img src='".$msg['profile_image_url']."' height='16'>";
	$screen_name=$msg['from_user'];
	echo "<a href='user.php?screen_name=".$screen_name."'>".$screen_name."</a>: ";
	$parsed = twitter_parse_tags($msg['text']);
	echo $parsed."\n";
	$id=$msg['id'];
	echo "<a href='re.php?id=".$id."'>re</a><br/></div>\n";
}

function show_dm_msg($i,$msg,$type){
	show_color_div($i,"ham");
	echo "<img src='".$msg['sender']['profile_image_url']."' height='16'>";
	$screen_name=$msg['sender']['screen_name'];
	echo "<a href='user.php?screen_name=".$screen_name."'>".$screen_name."</a>: ";
	$parsed = twitter_parse_tags($msg['text']);
	echo $parsed."\n";
	if($type=="inbox"){
		echo "<a href=\"inbox.php?screen_name=".$screen_name."\">re</a><br/></div>\n";
	} else {
		echo "<br/>\n</div>\n";
	}
}

/**
 * load translations from a po file 
 * 
 * @author felixding
 * @date 2010-03-09
 * @param string $lang the language pack
 * @return Array translated string
 */
function load_translations($lang = 'zh_cn') {
	//load the language pack
	if($lines = @file(SYSTEM_DIR.'/lang/'.$lang.'.po')) {
	
		$translations = array();
		
		foreach($lines as $line) {
		
			//trim line breaks		
			$line = trim($line);
			
			//find translation key
			if(preg_match('/(msgid)[[:space:]]+\"(.+)\"$/', $line, $match)) {
				$key = $match[2];
			}
			
			//find translation value			
			elseif(preg_match('/(msgstr)[[:space:]]+\"(.+)\"$/', $line, $match)) {
				$value = $match[2];
			}
			
			//merge them into translations array
			if($key && $value) {
				$translations[$key] = $value;
				unset($key, $value);
			}
			
		}
		
		//return
		return $translations;
	}
	
	//return nothing
	return null;
}

/**
 * translate a string
 * 
 * @author felixding
 * @date 2010-03-08
 * @param string $str the string to be translated 
 * @param boolean $return return the string or display directly
 * @return string the translated string
 */
function __($str, $return = false) {
	//load the language pack
	global $translations;
	
	if(isset($translations) && !empty($translations) && isset($translations[$str])) {
		
		$str = $translations[$str];
	}
	
	//display or return
	if($return) {
		return $str;
	}
	else {
		echo $str;
	}
}

/**
 * Works out the time since the entry post, takes a an argument in unix time (seconds) 
 * courtesy of unknown
 * 
 * @param int $timestamp unix time
 * @return string XX hours/minutes/seconds ago
 */
function time_since($timestamp) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , __('year', true)),
        array(60 * 60 * 24 * 30 , __('month', true)),
        array(60 * 60 * 24 * 7, __('week', true)),
        array(60 * 60 * 24 , __('day', true)),
        array(60 * 60 , __('hour', true)),
        array(60 , __('minute', true)),
    );
    
    $today = time(); /* Current unix time  */
    $since = $today - $timestamp;
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            // DEBUG print "<!-- It's $name -->\n";
            break;
        }
    }
    
    //$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    $name = ($name == __('month', true)) ? __('monthes', true) : $name;
    $print = ($count == 1) ? '1 '.$name : "$count$name";
    
    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];
        
        // add second item if it's greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1'.$name2 : ", $count2 $name2";
        }
    }
    return $print . __('ago', true);   
}
?>